if (window.plus) {
    ready();
} else {
    document.addEventListener('plusready', ready, false);
}
$(function(){
  if(AGENT_CLIENT.WEB){
    ready();
  }
})

function ready(){
  var
    id = 0,
    user=[],
    html=[],
    thisPage={};
    
  var deliveryPage = function(){
    this.init();
  };
  deliveryPage.prototype = {
    init:function(){
      this.initData();
      this.bindEvent();
    },
    initData:function(){
      user = getLocalStorage(STORAGE_MANAGER.user);
      if (!user) {
        redirect("../account/login.html");
        return false;
      }      
      this.initOrder();
    },
    initOrder:function(){
      id = getUrlParam("id");
      if(undefined == id){
        $.toast("没有订单号");
      }else{
        $.ajax({
          type:"get",
          url:CONFIG_MANAGER.RequstUrl + "v1/orders/" + id,
          async:true,
          headers: { "token": user.token },
          beforeSend: function () {
            $.showWaiting("等待中");
          }
        }).done(function(data){
          thisPage.loadDelivery(data);
        }).fail(function(data){
          $.toast(data);
        }).always(function(){
          $.closeWaiting();
        });
      }    
    },
    loadDelivery:function(data){
      
      html=[];
      html.push('<dl class="wlxxdh">');
      html.push('<dt><img src="" /></dt>');
      html.push('<dd>物流公司：<b>'+ data.express_name +'</b></dd>');
      html.push('<dd>运单号码:<b> '+ data.express_code +'</b></dd>');
      html.push('<dd>物流状态：<b>'+ data.status_text +'</b></dd>');
      html.push('</dl>');
      
      if(data.logs.length == 0){
      	html.push('<div class="zhanwushuju"><img src="../../images/zanwushuju.png"><br>暂无物流信息</div>');
      }else{
	      html.push('<div class="wlgzxxbg mbtopbottom">');
	      html.push('<div class="wlgzxxny">');      
	      for(var i=data.logs.length-1; i>-1; i--){
	        var logs = data.logs[i];
	        
	        html.push('<div class="wlxx ');
	        if((data.logs.length-1)==i){
	          html.push('wlxx1');
	        }
	        html.push('">');
	        html.push('<p class="wlxxlc ');
	        if((data.logs.length-1)==i){
	          html.push('wlxxlc1');
	        }
	        html.push(' mbbottom"><b class="dwyuan ');
	        if((data.logs.length-1)==i){
	          html.push('dwyuan1');
	        }
	        html.push('"></b>');
	        html.push(logs.record);
	        html.push('<br /><span>'+ toDateString(logs.create_time,'yyyy-MM-dd hh:mm:ss') +'</span></p>');
	        html.push('</div>');
	      };      
	      html.push('</div>');
	      html.push('</div>');
      }
      $("#main").html(html.join(''));
    },
    bindEvent:function(){
      $("#back").on("click", function(){ 
        back();
      });
    }
  };
  thisPage = new deliveryPage();
}
